package cn.edu.hnzj.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu.hnzj.pojo.User;
import cn.edu.hnzj.service.UserService;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @author:
 * @Copyright: https://group.cnblogs.com/learning/
 * @date: 2022-06-24 12:31:41
 */
@Controller
@RequestMapping("/users")
public class UserController {
	
	@Resource
	private UserService userService;
	/**
	 * 
	 * @MethodName: save
	 * @Description: 保存用户注册信息
	 * @param user
	 * @return String
	 * @date: 2022-06-23 08:56:49
	 */
	@RequestMapping("/register.do")
	public String save(User user ,HttpServletRequest request) {
		request.getSession().setAttribute("user", user);
		userService.save(user);
		return "redirect:/login.do";
	}

	/**
	 * @MethodName: userlogin
	 * @Description: 判断用户是否为空
	 * @param user
	 * @param request
	 * @param session
	 * @return
	 * @throws IOException String
	 * @date: 2022-06-26 04:31:29
	 */
	@RequestMapping("/login.do")
	public String userlogin(User user , HttpServletRequest request ,HttpSession session) throws IOException {
		// 处理请求
		if (userService.userlogin(user)) {
			request.getSession().setAttribute("user", user);
			return "redirect:/index.do";
		}else {
			session.setAttribute("msg", "用户名或密码错误，点击");
			request.setAttribute("user", user);
			return "forward:/login.do";
		}
		
	}
}
